﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using SL.MVVMS.RIA;
using MVVM_S.Samples.HelpDesk.Web.Models;
using MVVM_S.Samples.HelpDesk.Services;

namespace MVVM_S.Samples.HelpDesk.ViewModels
{
    /// <summary>
    /// This is a basic ViewModel Template using the MVVM-S Toolkit
    /// ViewModels inherit from the ViewModel base class, and implement
    /// service classes as needed, which provide data to the View Model
    /// You Should be able to assume that operations in the ViewModel 
    /// all occur on the UI thread, since the ServiceModel deals with
    /// threading.  Also, data should be blendable, if your service 
    /// implements a design data template as well.
    /// 
    /// Use the <see cref="slvmprop">slvmprop</see> snippet to create Properties in the ViewModel
    /// Use the <see cref="slvmcollection">slvmcollection</see> snippet to expose Collections
    /// Use the <see cref="slvmcmd">slvmcmd</see> snippet to create Commands in the ViewModel
    /// </summary>
    public class LookupsViewModel : ViewModel
    {
        HelpdeskService dataService;
        public LookupsViewModel()
        {
            dataService = new HelpdeskService();

            // DONE: Add setupCommand and setupCollection functions here
            // to construct those items as needed

            setupCategoryListCollection();
            setupActiveCategoryListCollection();
            setupOfficeListCollection();
            setupActiveOfficeListCollection();
            setupUserListCollection();
            setupActiveUserListCollection();

            loadAllLookups();
        }

        // DONE: Use the snippets slvmprop, slvmcollection, and slvmcmd to create 
        // Properties, Collections, and Commands for your View to bind to

        
        #region UserList - Collection of User
        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView ActiveUserList { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupActiveUserListCollection()
        {
            ActiveUserList = new PagedCollectionView(dataService.Users);
            using (ActiveUserList.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                ActiveUserList.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("DisplayName", ListSortDirection.Ascending));
                // UserList.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                ActiveUserList.Filter += (obj) =>    // Apply a filter
                {
                    User item = obj as User;
                    if (item == null)
                        return false;

                    // Apply your Filtering Logic Here by inspecting the User item.
                    if (item.Active)
                        return true;
                    else
                        return false;
                };
            }
        }

        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView UserList { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupUserListCollection()
        {
            UserList = new PagedCollectionView(dataService.Users);
            using (UserList.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                UserList.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("DisplayName", ListSortDirection.Ascending));
                // UserList.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                //UserList.Filter += (obj) =>    // Apply a filter
                //   {
                //       User item = obj as User;
                //       if (item == null)
                //           return false;

                //       // Apply your Filtering Logic Here by inspecting the User item.
                //       if (item.Active)
                //           return true;
                //       else
                //           return false;
                //   };
            }
        }

        /// <summary>
        /// Call this to Load and Refresh the UserList of User data afterwards
        /// </summary>
        void loadUserListCollection()
        {
            dataService.LoadLookups(() =>
                {
                    UserList.Refresh();
                });
        }
        #endregion
        
        #region CategoryList - Collection of Category
        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView ActiveCategoryList { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupActiveCategoryListCollection()
        {
            ActiveCategoryList = new PagedCollectionView(dataService.Categories);
            using (ActiveCategoryList.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                ActiveCategoryList.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("CategoryName", ListSortDirection.Ascending));
                // CategoryList.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                ActiveCategoryList.Filter += (obj) =>    // Apply a filter
                   {
                       Category item = obj as Category;
                       if (item == null)
                           return false;

                       // Apply your Filtering Logic Here by inspecting the Category item.
                       if (item.Active)
                           return true;
                       else
                           return false;
                   };
            }
        }
        
        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView CategoryList { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupCategoryListCollection()
        {
            CategoryList = new PagedCollectionView(dataService.Categories);
            using (CategoryList.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                CategoryList.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("CategoryName", ListSortDirection.Ascending));
                // CategoryList.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                // CategoryList.Filter += (obj) =>    // Apply a filter
                //    {
                //        Category item = obj as Category;
                //        if (item == null)
                //            return false;
                //
                //        // Apply your Filtering Logic Here by inspecting the Category item.
                //        if (item.PropertyName)
                //            return true;
                //        else
                //            return false;
                //    };
            }
        }

        /// <summary>
        /// Call this to Load and Refresh the CategoryList of Category data afterwards
        /// </summary>
        void loadCategoryListCollection()
        {
            dataService.LoadLookups(() =>
                {
                    CategoryList.Refresh();
                });
        }
        #endregion
        
        #region OfficeList - Collection of Office
        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView ActiveOfficeList { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupActiveOfficeListCollection()
        {
            ActiveOfficeList = new PagedCollectionView(dataService.Offices);
            using (ActiveOfficeList.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                ActiveOfficeList.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("OfficeName", ListSortDirection.Ascending));
                // OfficeList.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                ActiveOfficeList.Filter += (obj) =>    // Apply a filter
                {
                    Office item = obj as Office;
                    if (item == null)
                        return false;

                    // Apply your Filtering Logic Here by inspecting the Office item.
                    if (item.Active)
                        return true;
                    else
                        return false;
                };
            }
        }

        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView OfficeList { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupOfficeListCollection()
        {
            OfficeList = new PagedCollectionView(dataService.Offices);
            using (OfficeList.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                OfficeList.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("OfficeName", ListSortDirection.Ascending));
                // OfficeList.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                //OfficeList.Filter += (obj) =>    // Apply a filter
                //   {
                //       Office item = obj as Office;
                //       if (item == null)
                //           return false;

                //       // Apply your Filtering Logic Here by inspecting the Office item.
                //       if (item.Active)
                //           return true;
                //       else
                //           return false;
                //   };
            }
        }

        /// <summary>
        /// Call this to Load and Refresh the OfficeList of Office data afterwards
        /// </summary>
        void loadOfficeListCollection()
        {
            dataService.LoadLookups(() =>
                {
                    OfficeList.Refresh();
                });
        }
        #endregion

        /// <summary>
        /// Added custom function to load all Lookups at once
        /// and refresh all collections after that
        /// </summary>
        void loadAllLookups()
        {
            dataService.LoadLookups(() =>
            {
                CategoryList.Refresh();
                ActiveCategoryList.Refresh();
                UserList.Refresh();
                ActiveUserList.Refresh();
                OfficeList.Refresh();
                ActiveOfficeList.Refresh();

                // Notify any other ViewModels that this refresh was completed
                Notifications.SendNotification(this, "Notifications Refreshed", null, null);
            });
        }
                
                
    }
}
